/*
    m has x rows and y columns
*/


#include <stdio.h>
#include <stdlib.h>

void input_matrix ( int *m , int x, int y );

void matrix_simplify ( int *m, int x, int y );

void matrix_output ( int *m, int x, int z );

int main ( int argc, char const *argv[]) {
    int *m, x, y;
    printf("Please input the matrix's number of rows and number of columns\t");
    scanf("%d %d",&x,&y);
    if ( x < 1 || y < 1 ) {
        printf("err");
        return EXIT_FAILURE;
    }
    m = (int*)malloc(sizeof(int)*x*y);
    printf("Please input the matrix\n");
    input_matrix(m,x,y);
    matrix_simplify(m,x,y);
    matrix_output(m,x,y);
    free(m);
    return EXIT_SUCCESS; 
}

void input_matrix ( int *m, int x, int y ) {
    /* x is row, y is column.
    Please input in this order:
    1 2 3
    4 5 6
    7 8 9 
    */
    int num = 0;
    int *mp;
    mp = m;
    for ( num = 0; num < (x*y); num++ ) {
        scanf("%d",mp);
        mp++;
    }
}

void matrix_simplify ( int *m, int x, int y ) {
    int row, column1, column2;
    // int tmp[50] = {0}, i = 0;
    int *tmp,*tmpp;
    int *mp, *mp2;
    int flag = 0;
    int firstNum;
    int cnt = -1; /* cnt is the number of columns which don't contain 0 */
    for ( column1 = 0; column1 < y; column1++ ) {
        cnt++;
        
        /* check 0 in this column */
        for ( row = 0; row < x; row++ ) {
            mp = m + row*y;
            if ( *mp == 0 ) {
                mp += y;
            } else {
                firstNum = *mp;
                flag = 1;
                break;
            }
        }
        /* every number is 0, continue  */
        if ( flag == 0 ) {
            cnt--;
            continue;
        }
        /* queue */
        if ( row != cnt ) {
            tmp = (int*)malloc(sizeof(int)*y);
            tmpp = tmp;
            /* tmp stores numbers in the target row */
            for ( column2 = column1; column2 < y; column2++ ) {
                *tmpp = *mp;
                tmpp++;
                mp++;
            }
            /* target row stores numbers in the first row */
            mp2 = m + cnt * y;
            mp = m + row * y;
            for ( column2 = column1; column2 < y; column2++ ) {
                *mp = *mp2;
                mp++;
                mp2++;
            }
            /* first row stores numbers in tmp */
            mp2 = m + cnt * y;
            tmpp = tmp;
            for ( column2 = column1; column2 < y; column2++ ) {
                *mp2 = *tmpp;
                tmpp++;
                mp2++;
            }
            free(tmp);
            printf("-------------\n%d\n",*m);
        }
        /* simplify this row */
        mp = m + cnt * y;
        for ( column2 = column1; column2 < y; column2++ ) {    /* Bug 第二次输出1,矩阵row1变成  1   1*/
            (*mp) /= firstNum;
            mp++;
            printf("---------------------------\n%d\n",*m);
        }
        /* simplify other rows */
        flag = 0;
    }
}

void matrix_output ( int *m, int x, int y ) {
    int *mp;
    int row;
    int column;
    mp = m;
    printf("The answer is\n");
    for ( column = 0; column < y; column++ ) {
        mp = m + column;
        for ( row = 0; row < x; row++ ) {
            mp += row*y;
            printf("%5d",*mp);
        }
        printf("\n");
    }
}